TK!2
=v
#1
:n
L1
:v
10
:s
i
:c
spring length
#2
:n
L2
:v
10
:s
i
:c
   "     "
#3
:n
K1
:v
8
:s
i
:c
spring stiffness
#4
:n
K2
:v
1
:s
i
:c
   "      "
#5
:n
P1
:v
5
:s
i
:c
load in x1 direction
#6
:n
P2
:v
5
:s
i
:c
load in x2 direction
#7
:n
x1
:v
8.632066133347736
:s
i
:c
coordinate, unknown
#8
:n
x2
:v
4.531906698982808
:s
i
:c
coordinate, unknown
#9
:n
PE
:s
b
:c
potential energy, objective function
#10
:n
dd1
:v
6.320693275452382
:s
o
:c
dýPE/dx1ý
#11
:n
dd2
:v
3.258542956918225
:s
o
:c
dýPE/dx2ý
#12
:n
point
:v
'minimum
:s
o
:c
resulting point
=u
=l
#1
:n
val
:v
1
0
-1
E
#2
:n
mess
:v
'minimum
'saddle
'maximum
E
=f
#1
:n
springs
:c
Potential energy of a two-spring system
:t
Rule
:p
L1,L2,K1,K2,P1,P2
:a
x1,x2
:v
PE
#2
:n
dsprings
:c
Derivatives dPE/dx1 and dPE/dx2
:t
Rule
:p
L1,L2,K1,K2,P1,P2
:a
x1,x2,dPE%dx1,dPE%dx2
:v
PE
#3
:n
second
:c
Second derivatives
:t
Rule
:a
x1,x2,d2PE%dx12,d2PE%dx22,mess
#4
:n
message
:c
Solution status
:t
List
:d
val
:m
Table
:r
mess
#1
:n
springs
:z
pe1 = K1*(sqrt(x1^2+(L1-x2)^2) - L1)^2
pe2 = K2*(sqrt(x1^2+(L2+x2)^2) - L2)^2
PE = (pe1+pe2)/2 - P1*x1 - P2*x2

" Note: The function returns the potential energy of a two-spring system.
"       The springs with lengths L1 and L2 and stiffnesses K1 and K2 are
"       loaded with forces P1 and P2 along the x- and y-axes; the potential
"       energy is a function of the displacements x1 and x2.

"                                 P2
"                                ³
"                                ÀÄÄÄÄ P1
"                  ±oÄ@@@@@@@@@@ÄoÄ@@@@@@@@@@Äo±
/E
E
#2
:n
dsprings
:z
" Note:  The  %  symbol stands for  /  in the derivatives

aux1 = sqrt(x1^2+(L1-x2)^2) 
pe1 = K1*(aux1 - L1)^2
aux2 = sqrt(x1^2+(L2+x2)^2) 
pe2 = K2*(aux2 - L2)^2
PE = (pe1+pe2)/2 - P1*x1 - P2*x2

dpe1%daux1 = K1*2*(aux1 - L1)
daux1%dx1 = x1/aux1
daux1%dx2 = -(L1-x2)/aux1
dpe1%dx1 = dpe1%daux1 * daux1%dx1
dpe1%dx2 = dpe1%daux1 * daux1%dx2

dpe2%daux2 = K2*2*(aux2 - L2)
daux2%dx1 = x1/aux2
daux2%dx2 = (L1+x2)/aux2
dpe2%dx1 = dpe2%daux2 * daux2%dx1
dpe2%dx2 = dpe2%daux2 * daux2%dx2

dPE%dx1 = (dpe1%dx1+dpe2%dx1)/2 - P1
dPE%dx2 = (dpe1%dx2+dpe2%dx2)/2 - P2
/E
E
#3
:n
second
:z
" Notes:  1. This function evaluates second partial derivatives by 
"            numerical differentiation of the first derivatives.
"         2. The symbol  %  stands for  /  in the derivatives.

dx = 1e-6
call dsprings(x1,x2,d1,d2;PE)
call dsprings(x1*(1+dx),x2,d11,d21;PE)
call dsprings(x1,x2*(1+dx),d12,d22;PE)
d2PE%dx12 = (d11-d1)/(x1*dx)
d2PE%dx22 = (d22-d2)/(x2*dx)
d2PE%dx1dx2 = (d21-d1)/(x1*dx)
mess = message((d2PE%dx12*d2PE%dx22 - d2PE%dx1dx2^2 > 0) * sgn(d2PE%dx12))
/E
E
#4
:n
message
=r
#1
:r
" ***  Multidimensional Optimization, Part 1  ***
:s
C
#3
:r
" Some optimization problems in two or more dimensions may be solved
:s
C
#4
:r
" efficiently by taking partial derivatives of the objective function with
:s
C
#5
:r
" respect to unknown variables and equating them to zero.  This yields a
:s
C
#6
:r
" system of simultaneous equations which usually can be handled easily by
:s
C
#7
:r
" the Iterative Solver.
:s
C
#9
:r
" We will illustrate this approach with a problem of minimization of the
:s
C
#10
:r
" potential energy of a two-spring system from "Numerical Optimization
:s
C
#11
:r
" Techniques for Engineering Design" by G.N.Vanderplaats, McGraw-Hill, 1984,
:s
C
#12
:r
" pp. 72-73.  The problem is described and the objective function defined in
:s
C
#13
:r
" the function  springs  (see the Function Subsheet).
:s
C
#15
:r
" The unknowns (displacements) and the parameters of the problem (loads,
:s
C
#16
:r
" spring stiffnesses and lengths) are listed and defined in the Variable
:s
C
#17
:r
" Sheet.  The unknowns are function arguments, and the values of parameters 
:s
C
#18
:r
" are passed to the function as parameter variables.
:s
C
#20
:r
" The partial derivatives of the objective function  PE  with respect to the
:s
C
#21
:r
" unknowns  x1  and  x2  are derived in the function  dsprings .  For
:s
C
#22
:r
" convenience, the value of  PE  is calculated in this function as well, and
:s
C
#23
:r
" it is given the role of result variable. 
:s
C
#25
:r
" The whole minimization problem may then by expressed by a single rule:
:s
C
#27
:r
PE = dsprings(x1,x2,0,0)
:s
C
#29
:r
" (The last two arguments match the derivatives evaluated by the function;
:s
C
#30
:r
" if they were given names instead of 0's here, they and their values would
:s
C
#31
:r
" appear on the Variable Sheet).
:s
C
#33
:r
" At this point you may assign guess values, say, x1=x2=0, and solve (F9).
:s
C
#34
:r
" The function  dsprings , receiving zero values for partial derivatives,
:s
C
#35
:r
" becomes overconstrained, but the Iterative Solver manipulates the guesses
:s
C
#36
:r
" until the contradictions are reduced below the comparison tolerance at
:s
C
#37
:r
" which point the final values of  PE , x1  and  x2  are returned and
:s
C
#38
:r
" displayed in the Variable Sheet.
:s
C
#40
:r
" Strictly speaking, the condition of the first partial derivatives being
:s
C
#41
:r
" equal to zero may mean a maximum, a minimum or a saddle point.  Which case
:s
C
#42
:r
" holds can be found out by checking the values of the 2nd derivatives.  The
:s
C
#43
:r
" function  second  does that.  This function should be kept cancelled 
:s
C
#44
:r
" during the iteration.  Once the values of  x1  and  x2  are  calculated,
:s
C
#45
:r
" move them to the input field (by pressing I in their status fields),
:s
C
#46
:r
" uncancel the following rule, and solve again.
:s
C
#48
:r
call second(x1,x2,dd1,dd2,point)
:s
C
=p
=t
#1
:s
Screen
:v
Vertical
:f
1
:r
""
:c
""
:t
""
:u
#1
:l
abc
:w
10
:h
""
#2
:l
cba
:w
10
:h
""
#3
:l
xyz
:w
10
:h
""
#4
:l
prs
:w
10
:h
""
E
=n
=g
#1
:c
.000001
:i
10
:v
Yes
:l
60
:f
66
:m
0
:w
80
:b
Yes
:a
Yes
:r
Yes
:h
No
:t
1
:n
Yes
:d
LPT1
%Tr,f,0,1,0,1,0
